package com.tedu.weibo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tedu.weibo.common.result.JsonResult;
import com.tedu.weibo.common.result.StatusCode;
import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
@Validated
public class WeiboController {
    @Autowired
    private RedisTemplate<String, Serializable>redisTemplate;
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发表微博功能")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        log.info("WeiboController.insert(),发表微博");
        log.trace("开始执行insert方法");

        User user = (User) session.getAttribute("user");
        log.trace("通过session获取用户登录信息");
        log.debug("user:" + user);

        if (user == null) {
            log.trace("user为null,未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);

        } else {
            log.trace("user不为null，继续后续");
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            log.debug("weibo:" + weibo);
            weiboMapper.insert(weibo);
            log.trace("插入微博完毕");
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }

    }

    @ApiOperation("获取首页微博列表")
    @GetMapping("/selectIndex")
    @Bean
    public JsonResult selectIndexWeibo() throws Exception{
        String weiboList = (String) redisTemplate.opsForValue().get("weiboList");
        if (weiboList == null){
            List<WeiboIndexVO> list = weiboMapper.selectWeibo();
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writeValueAsString(list);
            redisTemplate.opsForValue().set("weiboList",jsonString);
            return JsonResult.success(list);
        }else {
            // 将json字符串转换为list
            ObjectMapper objectMapper = new ObjectMapper();

            List<WeiboIndexVO> list = objectMapper.readValue(weiboList, List.class);
            return JsonResult.success(list);
        }





    }

    @ApiOperation("获取微博详情")
//               name 指定方法参数名  value说明参数作用                  dataType参数类型
    @ApiImplicitParam(name = "id", value = "微博id", required = true, dataType = "Integer", example = "1")
    @GetMapping("/selectById")
    public JsonResult selectById(@Range(min = 1, max = 9999, message = "微博Id必须在1-9999之间") Integer id) {
        return JsonResult.success(weiboMapper.selectById(id));
    }



@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "微博id", required = true, dataType = "Integer", example = "1"),
        @ApiImplicitParam(name = "name", value = "用户昵称", required = true, dataType = "String", example = "张三")
})
    @GetMapping("/selectByIdAndName")
    public JsonResult selectByIdAndName(int id, String name) {
        return JsonResult.success(weiboMapper.selectById(id));
    }

}
